/**
 *  (c)  
 * <p>
 *
 * <p>
 * 
 */

package com.nn.smart.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nn.smart.bus.commons.mybatis.service.impl.BaseServiceImpl;
import com.nn.smart.bus.commons.tools.page.PageData;
import com.nn.smart.bus.commons.tools.utils.Result;
import com.nn.smart.bus.dao.OssDao;
import com.nn.smart.bus.entity.OssEntity;
import com.nn.smart.bus.service.OssService;
import com.nn.smart.bus.commons.tools.constant.Constant;
import com.nn.smart.bus.commons.tools.constant.NumConstant;
import com.nn.smart.bus.dto.UploadDTO;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;

@Service
public class OssServiceImpl extends BaseServiceImpl<OssDao, OssEntity> implements OssService {

    @Override
    public PageData<OssEntity> page(Map<String, Object> params) {
        IPage<OssEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                new QueryWrapper<>()
        );
        return getPageData(page, OssEntity.class);
    }

    @Override
    public Result<UploadDTO> afterUpload(String url, long size) {
        //保存文件信息
        OssEntity ossEntity = new OssEntity();
        ossEntity.setUrl(url);
        ossEntity.setCreateDate(new Date());
        this.insert(ossEntity);

        //文件信息
        UploadDTO dto = new UploadDTO();
        dto.setUrl(url);
        dto.setSize(size);

        return new Result<UploadDTO>().ok(dto);
    }

    @Override
    public void download(String fileUrl, HttpServletResponse response) {
        URL url;
        try {
            url = new URL(fileUrl);

            DataInputStream dataInputStream = new DataInputStream(url.openStream());
            if (null != dataInputStream) {

                String filename = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
                // 设定输出文件头
                response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));

                response.setCharacterEncoding("UTF-8");
                response.setHeader("Pragma", "No-Cache");
                response.setHeader("Cache-Control", "No-Cache");
                response.setDateHeader("Expires", 0);
                response.setContentType("application/msexcel; charset=UTF-8");

                byte[] buf = new byte[1024];
                int L;

                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                while ((L = dataInputStream.read(buf)) != NumConstant.ONE_NEG) {
                    toClient.write(buf, NumConstant.ZERO, L);
                }
                dataInputStream.close();
                toClient.flush();
                toClient.close();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
